const os = require("os");
const { exec } = require("child_process");
const { contextBridge } = require("electron");

// 内核版本信息
window.addEventListener("DOMContentLoaded", () => {
  ["chrome", "node", "electron"].forEach((item) => {
    console.log(`内核版本信息[${item}]: ${process.versions[item]}`);
  });
});

// 将electron的api绑定在window上从而提供给渲染器
contextBridge.exposeInMainWorld("electron", {
  // 获取网卡信息
  getNetworkInterfaces: () => {
    return os.networkInterfaces();
  },
  // 修改Mac地址
  setMacAddress: (interfaceName, newMacAddress) => {
    console.log(`${interfaceName}-${newMacAddress}`);
    return new Promise((resolve, reject) => {
      exec(`powershell.exe -Command "& { Set-NetAdapter -Name "${interfaceName}" -MacAddress "${newMacAddress}" -Confirm:$false}"`, (error, stdout, stderr) => {
        error && reject(error);
        resolve();
      });
    });
  },
});
